<template>
  <el-dialog title="新增角色" :visible.sync="showDialog" width="40%" @close="close">
    <el-form ref="form" :model="formData" :rules="formRules" label-width="140px" :inline="false" size="normal">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="formData.name" size="small" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="启用状态">
        <el-switch v-model="formData.state" size="mini" :active-value="1" :inactive-value="0" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="formData.description" size="small" placeholder="请输入" />
      </el-form-item>
    </el-form>

    <span slot="footer">
      <el-button size="small" @click="showDialog = false">取 消</el-button>
      <el-button size="small" type="primary" @click="btnOk">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addRoleApi } from '@/api/role'
export default {
  name: 'ADDRole',
  data() {
    return {
      // 弹框控制变量
      showDialog: false,
      // 表单变量
      formData: {
        name: '',
        description: '',
        state: 0 // 关闭 0 打开1
      },
      // 验证规则变量
      formRules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        description: [{ required: true, message: '请输入角色描述', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 确定提交
    async btnOk() {
      await this.$refs.form.validate()
      // 加工数据
      // 发送请求
      await addRoleApi(this.formData)
      // 提示成功
      this.$message.success('添加成功')
      // 关闭弹框
      this.showDialog = false
      // 列表更新
      this.$emit('updaeList')
    },
    // 弹框关闭
    close() {
      this.formData = {
        name: '',
        description: '',
        state: 0 // 关闭 0 打开1
      }
      this.$refs.form.clearValidate()
    }
  }

}
</script>

<style>

</style>
